home *** CD-ROM | disk | FTP | other *** search
- /*
- * Autoload.cmm
- *
- * This file handles most of the shell interface of CEnvi. The rest of the
- * interface is included as the file "ShellChr.cmm". This version will run
- * on all CEnvi systems known as of the revision date.
- *
- */
-
-
-
- if( !defined(_NWNLM_) && !defined(_DOS_) && !defined(_DOS32_) && !defined(_OS2_) &&
- !defined(_WINDOWS_) && !defined(_NTCON_) && !defined(_NTWIN_) )
- {
- printf("\nThe system you are running CEnvi on is not supported by this\n"
- "version of AUTOLOAD.CMM. A newer version should have been included\n"
- "in your distribution. You should use that instead.\n");
- exit(1);
- }
-
- if( !defined(_SHELL_) )
- {
- printf("Autoload.cmm is an internal shell implementation file and cannot be\n");
- printf("run as a program.\n");
- exit(EXIT_FAILURE);
- }
-
- /* ---------------------------------------------------------------------- */
-
- // First we have a list of commands that are to be passed on directly to
- // the shell. Windows and Netware have no such list of commands.
- if( defined(_DOS_) || defined(_DOS32_) )
- {
- SystemCommands =
- {
- "DIR", "COPY", "MKDIR", "MD", "RMDIR",
- "RD", "ERASE", "DEL", "TYPE", "ECHO"
- };
- }
- else if( defined(_OS2_) )
- {
- SystemCommands =
- {
- "DIR", "MKDIR", "MD", "RMDIR", "RD",
- "ERASE", "DEL", "TYPE", "START", "DETACH",
- "COPY", "ECHO"
- };
- }
- else if( defined(_NTCON_) )
- {
- SystemCommands =
- {
- "DIR", "COPY", "MKDIR", "MD", "RMDIR",
- "RD", "ERASE", "DEL", "TYPE", "ECHO",
- "START"
- };
- }
- if( defined(SystemCommands) ) qsort(SystemCommands,"stricmp");
-
-
- // Some Basic Aliases
- alias_list = {
- { "md", "makedir" },
- { "mkdir", "makedir" },
- { "clear", "cls" },
- { "erase", "del" },
- { "rm", "del" },
- { "cp", "copy" },
- { "mv", "move" },
- { "ren", "move" },
- { "rename", "move" },
- { "chmod", "attrib" },
- { "rd", "rmdir" },
- { "load", "start" }
- };
-
- if ( defined(_DOS_) || defined(_DOS32_) ) {
- // spawning can leave too-little memory, so increase COMSPEC size
- if ( !defined(COMSPEC_ENV_SIZE) )
- COMSPEC_ENV_SIZE = 2000;
- }
-
- ///////////////////////////////////////////////////////////////////////
-
-
- #include "CmdLine.lib"
-
- // The other systems all have this defined. Netware has no Environment variables
- // so we define it here.
-
- if( defined(_NWNLM_) ) PROMPT = NULL;
-
- if ( !defined(_WINDOWS_) && !defined(_NTWIN_)
- && defined(PROMPT) && strnicmp("[CEnvi]",PROMPT,7) )
- {
- strcpy(oldprompt,PROMPT);
- sprintf(PROMPT,"[CEnvi] %s",PROMPT);
- getenv();
- }
-
- /*
- * Change the current directory to the given one. Return 0 if successful.
- */
- my_chdir(newdir)
- {
- if( defined(_NWNLM_) ) return chdir(newdir);
- if( defined(_DOS_) || defined(_DOS32_) || defined(_WINDOWS_) )
- {
- lReg.ah = 0x3B;
- if ( !defined(_DOS32_) )
- lReg.ds = segment(newdir), lReg.dx = offset(newdir);
- else
- lReg.dx = pointer(newdir);
- interrupt(0x21,lReg,out);
- return out.ax==3;
- }
- if( defined(_NTCON_) || defined(_NTWIN_) )
- {
- return !DynamicLink("KERNEL32","SetCurrentDirectoryA",STDCALL,newdir);
- }
- if( defined(_OS2_) )
- {
- #define ORD_DOS32SETCURRENTDIR 255
- return DynamicLink("doscalls",ORD_DOS32SETCURRENTDIR,BIT32,CDECL,newdir);
- }
- }
-
- //**************************************************************************
- //****** BEGIN SECTION TO HANDLE INTERNAL COMMANDS FOR THE CLI SHELL *******
- //**************************************************************************
-
-
- // Next is a list of all commands handled internally to the shell. This list
- // must remain sorted. Also, the InternalHelp array must match. It is fortunate
- // that currently all the 'extra' commands only defined on certain systems
- // fall later than any other command in the alphabet.
-
- InternalCommands = {
- "ALIAS", "CD", "CHDIR", "CLS", "ECHO", "HELP", "PROMPT", "PWD"
- };
- InternalHelp = {
- "The ALIAS command lets you view or define aliases. ALIAS by itself will list\n"
- "all aliases currently defined. Otherwise define an alias by using the format:\n"
- "\n ALIAS name=text string",
- "CD used by itself will print the current directory. If you give it a\ndirectory name, it will change the current directory to it.",
- "CHDIR changes the current directory to the given directory.",
- "CLS clears the screen.",
- "ECHO prints some text to the screen.",
- "HELP by itself prints all known commands, otherwise it prints help on a command.",
- "PROMPT will change the prompt. By itself, it resets the prompt to the default.",
- "PWD will print the current working directory.",
- };
-
- if( defined(_DOS_) || defined(_DOS32_) || defined(_WINDOWS_) || defined(_NTCON_) ||
- defined(_NTWIN_) || defined(_OS2_) )
- {
- InternalCommands[GetArraySpan(InternalCommands)+1] = "SET";
- InternalHelp[GetArraySpan(InternalHelp)+1] =
- "SET allows you to view or change environment variables. SET by itself will\n"
- "list all such variables currently defined. Otherwise define one by using the\n"
- "format:\n\n"
- " SET variable=value";
- }
-
- if( defined(_WINDOWS_) || defined(_NTWIN_) || defined(_NWNLM_) )
- {
- InternalCommands[GetArraySpan(InternalCommands)+1] = "START";
- InternalCommands[GetArraySpan(InternalCommands)+1] = "TYPE";
- InternalHelp[GetArraySpan(InternalHelp)+1] =
- "START starts an Executable or NLM running asynchronously.";
- InternalHelp[GetArraySpan(InternalHelp)+1] =
- "TYPE will echo the contents of a file to the screen.";
- }
-
- // set up filter to handle each command input
- ShellFilterCommand("AutoloadFilterCommand");
-
- AutoloadFilterCommand(pCommand)
- {
- if ( !stricmp(pCommand,"EXIT") )
- {
- if( defined(oldprompt) ) PROMPT = oldprompt;
- } else {
- strcpy(tmp,pCommand);
- s = strchr(tmp,' ');
- if( s ) s[0] = '\0';
-
- if ( !stricmp(SplitFileName(tmp).name,"autoload") )
- {
- printf("Autoload.cmm is an internal shell implementation file and cannot be\n");
- printf("run as a program.\n");
- pCommand[0] = '\0';
- } else {
- strcpy(command,pCommand); pCommand[0] = '\0';
- ProcessCommandLine(command,"AutoloadFilterExecutor");
- }
- }
- }
-
- AutoloadFilterExecutor(pCommand)
- {
- lResult = EXIT_SUCCESS;
- undefine(alias);
- alias = "";
- for( i=0;pCommand[i];i++ )
- {
- if( isspace(pCommand[i]) ) break;
- alias[i] = pCommand[i];
- }
- the_rest = "";
- strcpy(the_rest,pCommand+i);
- alias[i] = 0;
- for( j=0;j<=GetArraySpan(alias_list);j++ )
- {
- if( !stricmp(alias,alias_list[j][0]) )
- {
- pCommand = "";
- strcpy(pCommand,alias_list[j][1]);
- strcat(pCommand,the_rest);
- break;
- }
- }
-
- // if line starts with a quote, it is cmm code to execute immediately.
- if ( strchr("\"\'`",pCommand[0]) ) {
- if( pCommand[strlen(pCommand)-1]==pCommand[0] )
- pCommand[strlen(pCommand)-1] = 0;
- lResult = interpret(pCommand+1,INTERP_TEXT|INTERP_NOINHERIT_GLOBAL|
- INTERP_NOINHERIT_LOCAL);
- pCommand[0] = 0;
- }
- // if command found in SystemCommands list then execute it immediately
- // with CMD.EXE then, else check internal cmds and some others
- else if ( 1 == sscanf(pCommand,"%s",lCommand) ) {
- if( defined(SystemCommands) && bsearch(lCommand,SystemCommands,"stricmp") )
- {
- strcpy(command,pCommand); pCommand[0] = '\0'
- if( defined(_OS2_) || defined(_NTCON_) || defined(_NTWIN_) )
- {
- lResult = spawn(P_WAIT,"CMD.EXE","/C",command);
- }
- else if( defined(_DOS_) || defined(_DOS32_) )
- {
- lResult = system(P_SWAP,command);
- }
- } else if ( bsearch(lCommand,InternalCommands,"stricmp") ) {
- sprintf(lInternalFunction,"InternalCommand_%s",strupr(lCommand));
- lParm = pCommand + strlen(lCommand);
- while( isspace(lParm[0]) ) lParm++;
- lResult = function(lInternalFunction,lParm);
- pCommand[0] = '\0';
- } else if ( !strcmp(lCommand+1,":") ) {
- // change drive
- if( defined(_OS2_) )
- {
- #define ORD_DOS32SETDEFAULTDISK 220
- DynamicLink("doscalls",ORD_DOS32SETDEFAULTDISK,BIT32,CDECL,
- toupper(lCommand[0])-'A'+1);
- lResult = EXIT_SUCCESS;
- }
- if( defined(_NTWIN_) || defined(_NTCON_) )
- {
- lResult = InternalCommand_CD(lCommand);
- }
- if( defined(_WINDOWS_) || defined(_DOS_) || defined(_DOS32_) )
- {
- lReg.ah = 0x0E;
- lReg.dl = toupper(lCommand[0]) - 'A';
- interrupt(0x21,lReg);
- lResult = EXIT_SUCCESS;
- }
- pCommand[0] = '\0';
- }
- }
- if ( pCommand[0] )
- {
- // no one else handled this command, so pass on to SPAWN
- if( defined(_DOS_) )
- {
- lResult = spawn(P_SWAP,pCommand);
- }
- else
- {
- lResult = spawn(P_WAIT,pCommand);
- }
- }
- if( lResult==-1 && pCommand[0] )
- {
- if( s = strchr(pCommand,' ') ) s[0] = '\0';
- filestruct = SplitFileName(pCommand);
- sprintf(badcommand,"%s%s",filestruct.name,filestruct.ext);
- printf("Unable to find program \"%s\" as an executable, batch, or cmm program\n"
- "anywhere in your path.\n",strupr(badcommand));
- }
-
- return lResult;
- }
-
- /* ---------------------------------------------------------------------- */
-
- // We have a number of internal commands.
-
- InternalCommand_ALIAS(param)
- {
- j = GetArraySpan(alias_list);
-
- if( param[0]=='\0' )
- {
- for( i=0;i<=j;i++ )
- printf("alias %s=%s\n",alias_list[i][0],alias_list[i][1]);
- return EXIT_SUCCESS;
- }
- for( i=0;param[i]!='\0';i++ )
- {
- if( param[i]=='=' )
- {
- param[i] = '\0';
- for( k=0;k<=j;k++ )
- if( !stricmp(param,alias_list[k][0]) )
- {
- strcpy(alias_list[k][1],param+i+1);
- return;
- }
- strcpy(alias_list[j+1][0],param);
- strcpy(alias_list[j+1][1],param+i+1);
- return EXIT_SUCCESS;
- }
- }
- for( i=0;i<=j;i++ )
- if( !stricmp(param,alias_list[i][0]) )
- {
- printf("alias %s=%s\n",alias_list[i][0],alias_list[i][1]);
- return EXIT_SUCCESS;
- }
- printf("alias %s is not defined.\n",param);
- return EXIT_FAILURE;
- }
-
-
- InternalCommand_CD(pParm)
- {
- // change directory
- if( !pParm[0] )
- {
- // no other argument, so just display current dir
- printf("%s\n",FullPath("."));
- } else {
- if( my_chdir(pParm) )
- printf("No such directory \"%s\".\n",pParm);
- }
- return EXIT_SUCCESS;
- }
-
-
- InternalCommand_CHDIR(param)
- {
- return InternalCommand_CD(param)
- }
-
-
- InternalCommand_CLS(pCommand)
- {
- ScreenClear();
- return EXIT_SUCCESS;
- }
-
-
- InternalCommand_ECHO(param)
- {
- printf("%s\n",param);
- return EXIT_SUCCESS;
- }
-
-
- InternalCommand_HELP(param)
- {
- if( param[0]!='\0' )
- {
- for( i=0;i<=GetArraySpan(InternalCommands);i++ )
- {
- if( !stricmp(param,InternalCommands[i]) )
- {
- printf("\n%s\n",InternalHelp[i]);
- return EXIT_SUCCESS;
- }
- }
- }
- printf("The CEnvi shell recognizes the following internal commands:\n\n");
- for( i=0;i<=GetArraySpan(InternalCommands);i++ )
- printf("%s\n",InternalCommands[i]);
- return EXIT_SUCCESS;
- }
-
-
- InternalCommand_PROMPT(param)
- {
- if( param[0]=='\0' )
- {
- PROMPT = "";
- } else {
- if( param[0]=='"' )
- {
- param++;
- s = strlen(param);
- if( param[s-1]=='"' ) param[s-1] = '\0'
- }
-
- strcpy(PROMPT,param);
- }
- getenv();
- return EXIT_SUCCESS;
- }
-
-
- InternalCommand_PWD(param)
- {
- printf("Current working directory is %s\n",FullPath("."));
- return EXIT_SUCCESS;
- }
-
-
- InternalCommand_SET(pParm)
- {
- if ( !pParm[0] ) {
- // no parameters so show entire SET
- EnvList = getenv();
- // sort alphabetically, just because we can
- qsort(EnvList,GetArraySpan(EnvList),"stricmp");
- for ( lIdx = 0; EnvList[lIdx]; lIdx++ )
- printf("%s=%s\n",EnvList[lIdx],getenv(EnvList[lIdx]));
- } else if ( !(lEqual = strchr(pParm,'=')) ) {
- // show single value if there is one
- if ( getenv(pParm) )
- printf("%s=%s\n",strupr(pParm),getenv(pParm));
- } else {
- // set to new value
- lEqual[0] = '\0';
- putenv(pParm,lEqual+1);
- }
- return EXIT_SUCCESS;
- }
-
-
- InternalCommand_START(pCommand)
- {
- lRet = spawn(P_NOWAIT,pCommand);
- if( -1 == lRet ) printf("Error in starting %s\n",pCommand);
- return lRet;
- }
-
-
- InternalCommand_TYPE(pCommand)
- {
- if( lFp = fopen(pCommand,"rt") )
- {
- while( lLine = fgets(lFp) )
- fputs(lLine,stdout);
- fclose(lFp);
- } else {
- printf("Unable to open file \"%s\"...\n",pCommand);
- }
- return EXIT_SUCCESS;
- }
-
- //************************************************************************
- //****** END SECTION TO HANDLE INTERNAL COMMANDS FOR THE CLI SHELL *******
- //************************************************************************
-
- if( defined(_NTWIN_) || defined(_WINDOWS_) )
- {
- ext_key_home = 0x24;
- ext_key_end = 0x23;
- ext_key_up = 0x26;
- ext_key_down = 0x28;
- }
- if( defined(_NTCON_) || defined(_DOS_) || defined(_DOS32_) || defined(_NWNLM_) ||
- defined(_OS2_) )
- {
- ext_key_home = 0x47;
- ext_key_end = 0x4F;
- ext_key_up = 0x48;
- ext_key_down = 0x50;
- }
- shellchr_alright = TRUE;
- #include <shellchr.cmm>
-